package com.tshopping.service.impl;

import com.tshopping.bean.Address;
import com.tshopping.dao.AddressDao;
import com.tshopping.service.AddressService;
import com.tshopping.util.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;

/**
 * @name: AddressServiceImpl
 * @description: 地址业务层类
 * @author: Jackeys
 * @date: 2020-12-24 17:36
 */
@Service(value = "addressService")
@Transactional
public class AddressServiceImpl implements AddressService {
    @Autowired
    private AddressDao addressDao;

    @Override
    /**
     * @description 添加地址
     * @Param: name
     * @Param: phone
     * @Param: address
     * @Param: user
     * @Param: province
     * @Param: city
     * @Param: district
     * @return  ResponseData
     * @author Jackeys
     * @date 2020-12-24 21:29
     */
    public ResponseData addAddress(String name, String phone, String address, int user, String province, String city, String district) {
        ResponseData responseData = ResponseData.ok();
        responseData.putDataValue("flag", addressDao.insertAddress(name, phone, address, user, province, city, district));
        return responseData;
    }

    @Override
    /**
     * @description 获取用户地址
     * @Param: user
     * @return  ResponseData
     * @author Jackeys
     * @date 2020-12-24 21:30
     */
    public ResponseData getUserAddresses(int user) {
        ResponseData responseData = ResponseData.ok();
        responseData.putDataValue("addressList", addressDao.selectUserOfAddressesByUserId(user));
        return responseData;
    }


    @Override
    /**
     * @description 修改用户地址
     * @Param: name
     * @Param: phone
     * @Param: address
     * @Param: province
     * @Param: city
     * @Param: district
     * @Param: id
     * @return  ResponseData
     * @author Jackeys
     * @date 2020-12-24 21:49
     */
    public ResponseData updateAddress(String name, String phone, String address, String province, String city, String district, int id) {
        ResponseData responseData = ResponseData.ok();
        responseData.putDataValue("flag", addressDao.updateAddressByAddressId(name, phone, address, province, city, district, id));
        return responseData;
    }

    @Override
    /**
     * @description 修改用户默认地址
     * @Param: newId
     * @Param: oldId
     * @return  ResponseData
     * @author Jackeys
     * @date 2020-12-25 18:40
     */
    public ResponseData updateDefault(int newId, int oldId) {
        ResponseData responseData = ResponseData.ok();
        responseData.putDataValue("flag", addressDao.updateAddressDefaultByAddressId(newId) && addressDao.updateCancelDefaultByAddressId(oldId));
        return responseData;
    }

    @Override
    /**
     * @description 添加默认地址
     * @Param: newId
     * @return  ResponseData
     * @author Jackeys
     * @date 2021-01-28 21:21
     */
    public ResponseData addDefault(int newId) {
        ResponseData responseData = ResponseData.ok();
        responseData.putDataValue("flag", addressDao.updateAddressDefaultByAddressId(newId));
        return responseData;
    }

    @Override
    /**
     * @description 删除用户地址
     * @Param: id
     * @return  ResponseData
     * @author Jackeys
     * @date 2020-12-25 18:46
     */
    public ResponseData deleteAddress(int id) {
        ResponseData responseData = ResponseData.ok();
        responseData.putDataValue("flag", addressDao.deleteAddressByAddressId(id));
        return responseData;
    }

    @Override
    /**
     * @description 返回用户默认地址
     * @Param: user
     * @return ResponseData
     * @author Jackeys
     * @date 2021-01-30 20:37
     */
    public ResponseData selectDefault(int user) {
        ResponseData responseData = ResponseData.ok();
        responseData.putDataValue("address", addressDao.selectDefaultAddressByUser(user));
        return responseData;
    }


}
